﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;

namespace Azylee.Core.DataUtils.EncryptUtils
{
    public class MD5Tool
    {
        /// <summary>
        /// 给一个字符串进行MD5加密
        /// </summary>
        /// <param name="s">待加密字符串</param>
        /// <returns>加密后的字符串</returns>
        public static string Encrypt(string s)
        {
            string result = "";
            try
            {
                byte[] buffer = Encoding.UTF8.GetBytes(s);
                HashAlgorithm algorithm = MD5.Create();
                byte[] hashBytes = algorithm.ComputeHash(buffer);
                result = BitConverter.ToString(hashBytes).Replace("-", "");
            }
            catch { }
            return result;
        }

        /// <summary>
        /// 给一个Byte数组进行MD5加密
        /// </summary>
        /// <param name="s">待加密字符串</param>
        /// <returns>加密后的字符串</returns>
        public static string Encrypt(byte[] buffer)
        {
            string result = "";
            try
            {
                HashAlgorithm algorithm = MD5.Create();
                byte[] hashBytes = algorithm.ComputeHash(buffer);
                result = BitConverter.ToString(hashBytes).Replace("-", "");
            }
            catch { }
            return result;
        }
    }
}
